package desmontec.aluguel

import grails.test.*
import desmontec.aluguel.dto.ConsumoMesDTO
import desmontec.testes.TesteIntegracao

class AluguelMaquinaControllerTest extends TesteIntegracao {

	static transactional = false
		
	def AluguelMaquinaService aluguelMaquinaService
	
    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }
	
	void testInserirDados(){
		this.inserirUsuario()
		this.inserirCliente1()
		this.inserirMaquina1()
		this.inserirMaquina2()
		this.inserirAluguel1()
		this.inserirAluguel2()
		this.inserirConsumosAluguel1()
	}
	
	
   void testAgruparConsumosMes(){
	   
	   AluguelMaquina aluguel = AluguelMaquina.get(1);
	   List<ConsumoMesDTO> listaConsumosMes = aluguelMaquinaService.agruparConsumosMes(aluguel);
	   
	   assertTrue(listaConsumosMes != null);
	   assertFalse(listaConsumosMes.isEmpty());
	   
   }
   
   
   void testAluguelValido(){
	   AluguelMaquina aluguel = AluguelMaquina.get(1);
	   
	   assertTrue (aluguel != null)
	   assertTrue( aluguelMaquinaService.aluguelValido(aluguel.fimValidade) )
   }
   
   
   void testAluguelInvalido(){
	   AluguelMaquina aluguel = AluguelMaquina.get(2);
	   
	   assertTrue (aluguel != null)
	   assertFalse (aluguelMaquinaService.aluguelValido(aluguel.fimValidade))
   }
   
   void testAtualizaStatusAlugueis(){
	   aluguelMaquinaService.atualizaStatusAlugueis()
	   def listaAtiva = aluguelMaquinaService.consultarAlugueisAtivos();
	   for (AluguelMaquina a: listaAtiva){
		   assertTrue("S".equals(a.flagAtivo))
	   }
   }
   
   void testCalcularConsumoMes(){
	   AluguelMaquina aluguel = AluguelMaquina.get(1);
	   def consumoMes = aluguelMaquinaService.agruparConsumosMes(aluguel)
	   def consumo = aluguelMaquinaService.calcularDetalhesConsumoMes(consumoMes)
	   
	   assertTrue(consumo != null);
	   assertEquals(8d, consumo.getMediaHoras());
	   assertEquals(50d, consumo.getMediaValorGasto());
	   
   }
   
   
   void testCalcularConsumoDias(){
	   AluguelMaquina aluguel = AluguelMaquina.get(1);
	   def consumoMes = aluguelMaquinaService.agruparConsumosMes(aluguel)
	   
	   assertTrue(consumoMes != null);
	   
	   def consumoMesAtual = consumoMes.get(0);
	   def consumoDias = consumoMesAtual.getConsumoDias();
	   def consumo = aluguelMaquinaService.calcularDetalhesConsumoDias(consumoDias);
	   
	   assertTrue(consumo != null);
	   assertEquals(8d, consumo.getMediaHoras());
	   assertEquals(50d, consumo.getMediaValorGasto());
	   
   }
   
  
   
   
      
}
